package com.wanxi.education.mapper;

import com.github.pagehelper.PageInfo;
import com.wanxi.education.model.pojo.History;
import com.wanxi.education.model.vo.catalogue.CatalogueVo;
import com.wanxi.education.model.vo.course.CourseByIdVo;
import com.wanxi.education.model.vo.course.CourseFindPageForPersonageVo;
import com.wanxi.education.model.vo.course.CourseFindPageVo;
import com.wanxi.education.model.vo.course.CourseHomeVo;
import com.wanxi.education.model.vo.history.HistoryAcceptVo;
import com.wanxi.education.model.vo.history.HistoryVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CourseFrontMapper {

/*    @Select("<script> SELECT * ,COUNT(id) course_duration FROM\n" +
            "(" +
            "SELECT a.* FROM (SELECT id,title,picture,price,teacher FROM course " +
            "WHERE delete_status=0" +
            "<if test='id!=null'> and id in(SELECT course_id FROM ccie_course_relation WHERE ccie_id in(select id from certificate where classification_id in (" +
            "<choose><when test = 'pid == true'>SELECT id FROM ccie_classification WHERE pid=#{id}</when><otherwise>#{id}</otherwise></choose>)))</if>" +
            ") a \n" +
            "JOIN catalogue b on b.course_id = a.id and pid!=0 and b.delete_status=0) a GROUP BY a.a.id </script>")*/
    List<CourseHomeVo> findPageForHome(CourseFindPageVo courseFindPageVo);

    @Select("SELECT *,count(*) course_duration FROM (SELECT a.id,a.title,a.introduce,a.teacher,a.picture,a.price FROM course a JOIN catalogue b on b.course_id=a.id and b.pid!=0 WHERE a.id=#{id}) a GROUP BY a.id")
    CourseByIdVo findByIdToHome(Long id);

    @Select("SELECT a.id,a.title,b.video_id FROM catalogue a LEFT JOIN video_directory_relation b on a.id = b.catalogue_id WHERE a.course_id = #{courseId} and a.pid=#{pid} ORDER BY a.`order` ")
    List<CatalogueVo> findCatalogueByCourseId(@Param("courseId") Long courseId, @Param("pid") Long pid);

    @Select("SELECT e.id,e.title,e.picture,e.price,e.teacher,count(f.course_id) course_duration FROM student_ccie_relation a JOIN" +
            " ccie_course_relation b on a.ccie_id = b.ccie_id JOIN " +
            "ccie_course_relation c on b.ccie_id = c.ccie_id JOIN " +
            "course e on c.course_id=e.id JOIN " +
            "catalogue f on f.course_id=e.id and f.pid!=0 WHERE a.user_id=#{id} and f.delete_status=0 GROUP BY f.course_id")
    List<CourseHomeVo> findPageForPersonage(Long id);

    @Select("SELECT a.id course_id,a.picture pcourse_pic,b.title catalogue_title,b.id catalogue_id FROM course a JOIN catalogue b on a.id=b.course_id WHERE b.id=#{id}")
    History findByCatalogueId(Long catalogueId);

    @Insert("Insert into history (user_id,course_id,course_pic,catalogue_title,catalogue_id,play_time,create_time,update_time) " +
            "value (#{userId},#{courseId},#{coursePic},#{catalogueTitle},#{catalogueId},#{playTime},now(),now())")
    void addHistoryVo(History history);

    @Select("select * from history where user_id=#{userId} and catalogue_id=#{catalogueId}")
    History findByUserIdAndCatalogueId(@Param("catalogueId") Long catalogueId, @Param("userId") Long userId);

    @Update("update history set user_id=#{userId},course_id=#{courseId},course_pic=#{coursePic},catalogue_title=#{catalogueTitle}" +
            ",catalogue_id=#{catalogueId},play_time=#{playTime},update_time=now() where id=#{id}")
    void updateHistoryVo(History history);

    @Select("select play_time from history where id = #{id}")
    String findVByIdTime(Long Id);

    @Select("select id,catalogue_id,catalogue_title,play_time from history where course_id = #{id} and user_id = #{userId} order by update_time")
    List<HistoryVo> findHistoryForPersonage(HistoryAcceptVo historyAcceptVo);

    @Select("select c.id,c.title,c.picture from recommend_course_relation r , course c where r.course_id = c.id and c.status = 0 and c.delete_status = 0")
    List<CourseHomeVo> findCarouselForHome();

    @Update("update course_info_relation set point = point + #{points} where id = #{courseId} ")
    void addCoursePoint(Long courseId ,Integer points);
}
